package com.sgcc.fj.pmos.e_fj_personalcenter.activity;

import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.sgcc.fj.pmos.arouter.mall.MallArouterPath;
import com.sgcc.fj.pmos.arouter.personalcenter.PersonalCenterArouterPath;
import com.sgcc.fj.pmos.common.base.MvpBaseRecycleActivity;
import com.sgcc.fj.pmos.common.bean.BaseResponse;
import com.sgcc.fj.pmos.common.bean.EventCenter;
import com.sgcc.fj.pmos.common.bean.PackageBean;
import com.sgcc.fj.pmos.common.utils.ArguConstant;
import com.sgcc.fj.pmos.common.utils.DialogUtil;
import com.sgcc.fj.pmos.common.utils.PackageContrastUtils;
import com.sgcc.fj.pmos.common.utils.UserManager;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.fj.pmos.e_fj_personalcenter.adapter.FootprintAdapter;
import com.sgcc.fj.pmos.e_fj_personalcenter.contract.FootprintContract;
import com.sgcc.fj.pmos.e_fj_personalcenter.presenter.FootprintPresenter;

import java.util.List;

/**
 * 历史浏览
 */
@Route(path = PersonalCenterArouterPath.PERSONALCENTER_FOOTPRINT)
public class FootprintActivity extends MvpBaseRecycleActivity<PackageBean, FootprintPresenter> implements FootprintContract.View {

    private FootprintAdapter mFootprintAdapter;

    @Override
    public String getTitleText() {
        return "历史浏览";
    }

    @Override
    public void querySuccess(List<PackageBean> packageFootprintList) {
        loadSuccess(false, packageFootprintList);
    }

    @Override
    public void addCollectionFailed() {

    }


    @Override
    public void addCollectionSuccess(BaseResponse response, String combId) {
        if (response != null) {
            if (response.isSuccess()) {
                List<PackageBean> list = mFootprintAdapter.getData();
                int index = 0;
                for (int i = 0; i < list.size(); i++) {
                    if (combId.equals(list.get(i).getCombId())) {
                        list.get(i).setUstatus("01");
                        index = i;
                    }
                }
                mFootprintAdapter.notifyItemChanged(index);

            }
        }
    }

    @Override
    public void deletePackageByIdSuccess(BaseResponse response, String combId) {
        if (response != null) {
            if (response.isSuccess()) {
                List<PackageBean> list = mFootprintAdapter.getData();
                int index = 0;
                for (int i = 0; i < list.size(); i++) {
                    if (combId.equals(list.get(i).getCombId())) {
                        list.get(i).setUstatus("00");
                        index = i;
                    }
                }
                mFootprintAdapter.notifyItemChanged(index);
            }
        }
    }


    @Override
    protected void initViews() {
        super.initViews();
        title_bar.setRightIcon(R.mipmap.go_main, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goMainActivity();
            }
        });
    }

    @Override
    public void queryFailed() {
        loadFalied();
    }

    @Override
    protected FootprintPresenter createPresenter() {
        return new FootprintPresenter();
    }

    @Override
    public boolean isBindEventBusHere() {
        return true;
    }

    @Override
    public void onEventMainThread(EventCenter eventCenter) {
        super.onEventMainThread(eventCenter);

        if (eventCenter.getEventCode() == ArguConstant.REFRESH_CONTRAST) {
            mFootprintAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public BaseQuickAdapter getAdapter() {
        mFootprintAdapter = new FootprintAdapter(mContext);
        mFootprintAdapter.setOnItemClickListener(new FootprintAdapter.OnItemClickListener() {
            @Override
            public void OnAddCollectionListener(int position, PackageBean bean) {
                if (UserManager.toLogin()) {
                    mPresenter.addUserPackageCollection("", bean.getMembersSdId(), bean.getMembersSdName(),
                            bean.getCombId(), bean.getCombName());
                }
            }

            @Override
            public void OnCancleCollectionListener(int position, PackageBean bean) {
                mPresenter.deletePackageById(bean.getCombId(), bean.getMembersSdId());
            }

            @Override
            public void OnItemsClickListener(int position, PackageBean bean) {
                ARouter.getInstance()
                        .build(MallArouterPath.MALL_PACKAGEDETAILS)
                        .withString("combName", bean.getCombCode())
                        .withBoolean("isBlue", "1".equals(bean.getpNum()) && ("03".equals(bean.getMainType()) || "04".equals(bean.getMainType())) &&
                                "0".equals(bean.getMainItemType()) &&
                                ("0".equals(bean.getMainDifType()) || TextUtils.isEmpty(bean.getMainDifType())))
                        .navigation();

            }

            @Override
            public void OnDeleteClickListener(int position, PackageBean s) {
                DialogUtil.showConfirm(mContext, "提示", "确认要删除这条足迹吗?",
                        new OnConfirmListener() {
                            @Override
                            public void onConfirm() {
                                mFootprintAdapter.remove(s);
                                PackageContrastUtils.deleteFootprint(s.getCombId());
                            }
                        });

            }
        });
        return mFootprintAdapter;
    }

    @Override
    public void getDataRequest(int listSize, boolean isloading) {
        super.getDataRequest(listSize, isloading);
        mPresenter.getFootprintList(listSize, isloading);

    }
}
